package org.jeecg.modules.demo.crm.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.demo.crm.entity.CMaterial;
import org.jeecg.modules.demo.crm.mapper.CWeixinMapper;
import org.jeecg.modules.demo.crm.service.ICMaterialService;
import org.jeecg.modules.demo.crm.service.ICWeixinService;
import org.jeecg.modules.demo.crm.util.wx.WxLoginRequest;
import org.jeecg.modules.demo.crm.util.wx.WxService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description: 微信
* @Author: jeecg-boot
* @Date:   2025-06-02
* @Version: V1.0
*/
@Api(tags="微信")
@RestController
@RequestMapping("/wx")
@Slf4j
public class WeixinController extends JeecgController<CMaterial, ICMaterialService> {


    @Autowired
    private WxService wxService;
    @Autowired
    private ICWeixinService weixinService;

    @PostMapping("/wxLogin")
    @IgnoreAuth
    public Result<JSONObject> wxLogin(@RequestBody WxLoginRequest request) {
        //System.out.println("=======授权请求参数=========");
        System.out.println(JSONObject.toJSONString(request));
        JSONObject wxRes = wxService.getOpenIdByCode(request.getCode());
        if (!wxRes.containsKey("openid")) {
            return Result.error("微信授权失败：" + wxRes.getString("errmsg"));
        }
        JSONObject obj = weixinService.getWxLogin(wxRes);
        Result<JSONObject> result = new Result<JSONObject>();
        if(obj != null){
            result.setResult(obj);
            result.setSuccess(true);
            result.setCode(200);
        }else {
            result.setMessage("用户不存在，登陆后绑定");
            result.setSuccess(false);
            result.setCode(500);
        }
        return result;
    }

    @PostMapping("/wxBind")
    public Result<String> wxBind(@RequestBody WxLoginRequest request) {
        System.out.println(JSONObject.toJSONString(request));
        JSONObject wxRes = wxService.getOpenIdByCode(request.getCode());
        if (wxRes.containsKey("errcode")) {
            return Result.error("微信授权失败：" + wxRes.getString("errmsg"));
        }
        weixinService.wxBind(wxRes);
        return Result.ok("绑定成功");
    }
    @PostMapping("/relieveWx")
    public Result<String> relieveWx() {
        weixinService.relieveWx();
        return Result.ok("解除绑定成功");
    }





}
